<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/17
 * Time: 16:52
 */

namespace Admin\Controller;


use Admin\Common\CommonController;

/**
 * Class CategoryController
 * @package Admin\Controlle
 * 分类管理
 */
class CategoryController extends CommonController
{
     public function lists(){
    $category=D('category')->alldata();
         $this->assign('category',$category);
//         dd($category);
     $this->display();
     }
    /**
     * 分类添加
     */
    public function add(){
        $cid=I('get.cid');
        if(IS_POST){
            $_POST['aname']=session('aname');
            $_POST['addtime']=time();
          $res=D('category')->store(I('post.'));
            $this->jump($res,'lists');
        }
        //找出属性值
        $nature=D('nature')->alldate();
        $this->assign('nature',$nature);
    $this->display();
    }

    /**
     * 分类编辑
     */
      public function edit(){
          $cid=I('get.cid');
          if(IS_POST){
              $_POST['cid']=$cid;
              dd($_POST);
              $res=D('category')->store(I('post.'));
              $this->jump($res,'lists');
          }
          //找出旧数据
          $olddate=M('category')->where("cid=$cid")->find();
         $this->assign('olddate',$olddate);
          //找出属性值
          $nature=D('nature')->alldate();
          $this->assign('nature',$nature);
          dd($olddate);
         $this->display();
      }

    /**
     * 删除
     */
    public function del(){
       $cid=I('get.cid');
//        找出改分类的第一子类
        $son=M('category')->where("sid=$cid")->getField('cid',true);
        if($son){
            $this->error('对不起该类有子类,请删除子类');die;
        }else{
            $res=M('category')->where("cid=$cid")->delete();
            if($res){
              $this->success('删除成功',U('lists'));die;
            }else{
                $this->error('删除失败');die;
            }
        }

    }
}